const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const ManifestPlugin = require('webpack-manifest-plugin')

class MyPlugin {
  apply (compiler) {
    console.log('my plugin 启动')

    compiler.hooks.emit.tap('MyPlugin', compilation => {
      // compilation 可以立即为此次打包过程的上下文
      for(const name in compilation.assets) {
        // console.log(name)
        // console.log(compilation.assets[name].source())
        if(name.endsWith('.js')) {
          const contents = compilation.assets[name].source()
          const withoutComments = contents.replace(/\/\*\*+\*\//g, '')
          compilation.assets[name] = {
            source: () => withoutComments,
            size: () => withoutComments.length
          }
        }
      }
    })
  }
}

module.exports = {
  mode: 'none',
  entry: './src/main.js',
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
    // publicPath: 'dist/'
  },
  devtool: 'cheap-module-eval-source-map',
  devServer: {    
    // 额外为开发服务器指定查找资源目录
    contentBase: './public',
    proxy: {
      '/api': {
        // http://localhost:8080/api/users ->  https://api.github.com/api/users
        target: 'https://api.github.com',
        // http://localhost:8080/api/users ->  https://api.github.com/users
        pathRewrite: {
          '^/api': ''
        },
        // 不需要localhost:8080作为请求github的主机名
        changeOrigin: true
      }
    },
    // hot: true,
  },
  module: {
    rules: [
      {
        test: /.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /.js$/,
        exclude: [/node_modules/, /dist/],
        use: {
          loader: "eslint-loader",
          options: {
            fix: true
          }
        },
        enforce: 'pre'
      },
      {
        test: /.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      // {
      //   test: /.png$/,
      //   use: 'file-loader'
      // },  // 如果使用  url-loader  这个可以不写，但是还是必须安装
      {
        test: /.png$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10 * 1024 // 10kb  10kb以下
          }
        }
      },
      // {
      //   test: /.html$/,
      //   use: {
      //     loader: 'html-loader',
      //     options: {
      //       attrs: ['img:src', 'a:href']
      //     }
      //   }
      // },
      {
        test: /.md$/,
        use: [
          "html-loader",
          "./src/markdown-loader"
        ]
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    // 用于生产index.html
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'public/index.html',
      title: 'Webpack Plugin Sample1245',
      meta: {
        viewport: 'width-device-width'
      }
    }),
    // 用于创建about.html
    new HtmlWebpackPlugin({
      filename: 'about.html',
      template: 'public/index.html',
    }),
    // 开发过程中最好不使用这个插件
    // new CopyWebpackPlugin({
    //   patterns: [
    //     {
    //       from: path.join(__dirname, 'public'),
    //       to: ''
    //     }
    //   ]
    // }),
    // new MyPlugin()
    // new webpack.HotModuleReplacementPlugin()
    new ManifestPlugin()
  ]
}